<?php

namespace App\Model;

//use Illuminate\Database\Eloquent\Model;

class Menu extends Model
{
    // 软删除 字段
    public $dates = ['deleted_at'];
    // 允许显示的字段
    protected $fillable = ['id','pid','name','permission','admin_id','order','created_at','updated_at'];
    //隐藏的字段

    static public function _validate(){
        return array(
            'pid'=>'required|max:10',
            'permission'=>'required|max:10|exists:permissions,id',
            'admin_id'=>'required|max:10',
            'name'=>'required|max:32',
            'order'=>'required|max:10'
        );
    }

    static public function MenuAdd($data){
        $data = self::validate($data);
        if(!$data || !$data['status']){
            return $data;
        }
        $data['data']['admin_id'] = auth()->user()->id;
//        dd($data['data']);
        $data = self::create($data['data']);
        if($data){
            return Json::success($data);
        }
        return Json::error('添加失败');
    }


    static public function Menuset($data){
        $id = $data['id'];
        $data = self::validate($data);
        if(!$data || !$data['status']){
            return $data;
        }
        $menu = self::find($id);
        $data['data']['admin_id'] = auth()->user()->id;
        foreach($data['data'] as $key=>$val){
            $menu->$key = $val;
        }
        if($menu->save()){
            return Json::success('修改成功');
        }
        return Json::error('修改失败');
    }



//    static public function getAll(){
//        $menus = self::orderBy('id','desc')->get();
//        $data = $menus->toArray();
//        foreach($data as $menu){
//            if(!$menu['order']){
//                $menu['order']  = $menu['id'];
//            }
////            $i = -1;
////            do{
////                $i++;
////            }while(!isset($tmp[$menu['order']+$i]));
//            $tmp[$menu['order']] = $menu;
//        }
//        unset($data);
//        foreach($tmp as $val){
//            if($val['pid']){
//                if(isset($data[$val['id']])){
//                    ksort($data[$val['id']]);
//                    $val['children'] = $data[$val['id']];
//                    unset($data[$val['id']]);
//                }
//                $data[$val['pid']][$val['order']] = $val;
//            }else{
//                if(isset($data[$val['id']])){
//                    ksort($data[$val['id']]);
//                    $val['children'] = $data[$val['id']];
//                    unset($data[$val['id']]);
//                }
//                $data[$val['order']] = $val;
//            }
//        }
//        return $data;
//    }

    static public function Permission($menus){
        $ids = array();
        foreach($menus as $val){
            if($val['permission']){
                $ids[] = $val['permission'];
            }
        }
        if($ids){
            $permission = Permission::whereIn('id',$ids)->get()->toArray();
        }
        $tmp = array();
        foreach($permission as $val){
            $tmp[$val['id']] = $val;
        }
        foreach($menus as &$val){
            if(isset($tmp[$val['permission']])){
                $val['permission'] = $tmp[$val['permission']];
            }
        }
        return $menus;
    }


    // 对 菜单 层级计划
    static public function getMenus($data){
        $tmp = $data;
        $data = array();
        foreach($tmp as $key=>$val){
            if($val['pid']){
                if(isset($data[$val['id']])){
                    $val['children'] = array_values(array_sort($data[$val['id']],function($val){
                        return $val['order'];
                    }));
                    unset($data[$val['id']]);
                }
                $data[$val['pid']][$key] = $val;
            }else{
                if(isset($data[$val['id']])){
                    $val['children'] = array_values(array_sort($data[$val['id']],function($val){
                        return $val['order'];
                    }));
                    unset($data[$val['id']]);
                }
                $data[$val['id']] = $val;
            }
        }
        $data = array_values(array_sort($data,function($val){
            return $val['order'];
        }));
        return $data;
    }


    static public function Permission_menu($permission){
        $ids = array_keys($permission);
        $ids[] = 0;
        $ids[] = '';
        $menu = self::whereIn('permission',$ids)->orderBy('id','desc')->get()->toArray();
        $route = request()->attributes->get('route');
        foreach($menu as &$val){
            if(isset($m)){
                if($val['id'] == $m['pid']){
                    $val['click'] = 1;
                    if($val['pid']){
                        $m = $val;
                    }
                }
            }else{
                if($val['permission'] == $route['id']){
                    $val['click'] = 1;
                    $m = $val;
                }
            }
            if($val['permission']){
                if(isset($permission[$val['permission']])){
                    $val['permission'] = $permission[$val['permission']]['action'];
                }else{
                    $val['permission'] = $permission[1]['action'];
                }
            }
        }
        $menu = self::getMenus($menu);
        ksort($menu);
        return $menu;
    }

    // 删除方法
    static public function Menu_Del($id){
        $menu = self::find($id);
        if(!$menu){
            return Json::error('获取失败');
        }
        $menu->admin_id = auth()->user()->id;
        $menu->save();
        if($menu->delete()){
            return Json::success('删除成功');
        }
        return Json::error('删除失败');




    }
}
